//
//  Utils.h
//
//  Created by dybai on 2016-08-10.
//

#include "Utils.h"
#include "LogUtils.h"


Utils::Utils(void)
{
}


Utils::~Utils(void)
{
}

std::vector<CString> Utils::split(CString origin, CString sep)
{
	std::vector<CString> strVec;
	while (!origin.IsEmpty())
	{
		CString line = origin.SpanExcluding(sep);
		if (!line.IsEmpty())
		{
			strVec.push_back(line);
		}
		origin = origin.Right(origin.GetLength() - line.GetLength() - 1);
	}
	return strVec;
}

int Utils::createDir(CString &path)
{
	if (PathIsDirectory(path) == FILE_ATTRIBUTE_DIRECTORY) // Directory is exist
	{
		LOGD("Directory is exist: %s", path);
		return 0;
	}
	else
	{
		if (CreateDirectory(path, NULL) == TRUE) // Directory create success
		{
			LOGD("Directory create success\n");
			return 0;
		}
		else
		{
			LOGE("Directory create fail\n");
			return -1;
		}
	}
}

DWORD Utils::execWait(LPSTR lpCommandLine, LPSTARTUPINFOA lpStartupInfo)
{
	DWORD result = 0;
	PROCESS_INFORMATION pinfo;
	if (FALSE == CreateProcess(NULL, lpCommandLine, NULL, NULL, TRUE, 0, NULL, NULL, lpStartupInfo, &pinfo))
	{
		result = GetLastError();
	}
	WaitForSingleObject(pinfo.hProcess, INFINITE);
	CloseHandle(pinfo.hProcess);
	CloseHandle(pinfo.hThread);
	return result;
}

CString Utils::GetSystemFolderPath(int csidl)
{
	CString path;

	::SHGetFolderPath(NULL, csidl, NULL, 0, path.GetBuffer(MAX_PATH));
	path.ReleaseBuffer();

	return path;
}

CString Utils::GetAppDataPath()
{
	return GetSystemFolderPath(CSIDL_LOCAL_APPDATA);
}

CString Utils::GetUserDesktopPath()
{
	return GetSystemFolderPath(CSIDL_DESKTOPDIRECTORY);
}
